const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    list: [],
    load: true,
    leftname:[],
    list:[{}],
    totalCost:150,
    orderNumber:0,
    activeIndex:4,
    scrollTop:100,
    toView:'a0',
    cartList:[],
    currentType:0,
    currentIndex:0,
    sumMoney:0,
    showCart:false,
    toastHidden:true,
    swiperImg: [
      { src: "https://www.heytea.com/img/bg1.jpg" },
      { src: "https://www.heytea.com/img/banner1.jpg" },
      { src: "https://www.heytea.com/img/banner2.jpg" },
      { src: "https://www.heytea.com/img/banner3.jpg" }
    ],

  },

    //提交订单
  submitClick:function(){
    if(this.data.sumMoney!=0){
      wx.setStorageSync('cartList', this.data.cartList);  //购物车列表
      wx.setStorageSync('sumMoney', this.data.sumMoney);  //价格
      wx.setStorageSync('orderNumber', this.data.orderNumber);   //数量
      wx.navigateTo({
        url: '../list/list'
      }) 
    }
    if(this.data.sumMoney == 0){
    }
  },

  addToCart1: function (e) {
    // console.info("添加");
    // console.info(e.currentTarget.dataset);
    var type = e.currentTarget.dataset.type;
    this.setData({
      currentType: type,
    });
    var a = this.data
    var addItem = {
      "name": a.type1[a.currentType].name,
      "price": a.type1[a.currentType].price,
      "number": 1,
      "sum": a.type1[a.currentType].price,
      "id": a.type1[a.currentType].id,
    }
    var sumMoney = a.sumMoney + a.type1[a.currentType].price;
    var cartList = this.data.cartList;
    cartList.push(addItem);  //
    this.setData({
      cartList: cartList,
      showModalStatus: false,
      sumMoney: sumMoney,
      orderNumber: a.orderNumber + 1
    });
    console.log(this.data.cartList)
    this.setData({
      toastHidden:false
    });
    
    var _this = this;
    setTimeout(function () {
      _this.setData({
        toastHidden: true
      });
    }, 1000);
  },

  addToCart2: function (e) {
    // console.info("添加");
    // console.info(e.currentTarget.dataset);
    var type = e.currentTarget.dataset.type;
    this.setData({
      currentType: type,
    });
    var a = this.data
    var addItem = {
      "name": a.type2[a.currentType].name,
      "price": a.type2[a.currentType].price,
      "number": 1,
      "sum": a.type2[a.currentType].price,
      "id": a.type2[a.currentType].id,
    }
    var sumMoney = a.sumMoney + a.type1[a.currentType].price;
    var cartList = this.data.cartList;
    cartList.push(addItem);  //
    this.setData({
      cartList: cartList,
      showModalStatus: false,
      sumMoney: sumMoney,
      orderNumber: a.orderNumber + 1
    });
    console.log(this.data.cartList)
    this.setData({
      toastHidden:false
    });
    
    var _this = this;
    setTimeout(function () {
      _this.setData({
        toastHidden: true
      });
    }, 1000);
  },
  addToCart3: function (e) {

    console.info("添加");
    console.info(e.currentTarget.dataset);

    var type = e.currentTarget.dataset.type;
    this.setData({
      currentType: type,
    });
    var a = this.data
    var addItem = {
      "name": a.type3[a.currentType].name,
      "price": a.type3[a.currentType].price,
      "number": 1,
      "sum": a.type3[a.currentType].price,
      "id": a.type3[a.currentType].id,
    }
    var sumMoney = a.sumMoney + a.type1[a.currentType].price;
    var cartList = this.data.cartList;
    cartList.push(addItem);  //
    this.setData({
      cartList: cartList,
      showModalStatus: false,
      sumMoney: sumMoney,
      orderNumber: a.orderNumber + 1
    });
    console.log(this.data.cartList)
    this.setData({
      toastHidden:false
    });
    
    var _this = this;
    setTimeout(function () {
      _this.setData({
        toastHidden: true
      });
    }, 1000);
  },

  addToCart4: function (e) {

    console.info("添加");
    console.info(e.currentTarget.dataset);

    var type = e.currentTarget.dataset.type;
    this.setData({
      currentType: type,
    });
    var a = this.data
    var addItem = {
      "name": a.type4[a.currentType].name,
      "price": a.type4[a.currentType].price,
      "number": 1,
      "sum": a.type4[a.currentType].price,
      "id": a.type4[a.currentType].id,
    }
    var sumMoney = a.sumMoney + a.type1[a.currentType].price;
    var cartList = this.data.cartList;
    cartList.push(addItem);  //
    this.setData({
      cartList: cartList,
      showModalStatus: false,
      sumMoney: sumMoney,
      orderNumber: a.orderNumber + 1
    });
    console.log(this.data.cartList)
    this.setData({
      toastHidden:false
    });
    
    var _this = this;
    setTimeout(function () {
      _this.setData({
        toastHidden: true
      });
    }, 1000);
  },


  showCart:function(){
      console.log(this.data.showCart)
      if (this.data.cartList.length != 0) {
        this.setData({
          showCart: !this.data.showCart,
        });
      }
  },
  clearCart:function(){
      this.setData({
        cartList:[],
        showCart:false,
        sumMoney:0,
        orderNumber:0,
      })
  },
  addNumber: function (e) {
      var index = e.currentTarget.dataset.index;
      console.log(index)
      var cartList = this.data.cartList;
      cartList[index].number++;
      var sum = this.data.sumMoney + cartList[index].price;
      cartList[index].sum += cartList[index].price;
  
      this.setData({
        cartList: cartList,
        sumMoney: sum,
        orderNumber: this.data.orderNumber + 1
      });
  },

  decNumber: function (e) {
      var index = e.currentTarget.dataset.index;
      console.log(index)
      var cartList = this.data.cartList;
  
      var sum = this.data.sumMoney - cartList[index].price;
      cartList[index].sum -= cartList[index].price;
      cartList[index].number == 1 ? cartList.splice(index, 1) : cartList[index].number--;
      this.setData({
        cartList: cartList,
        sumMoney: sum,
        showCart: cartList.length == 0 ? false : true,
        orderNumber: this.data.orderNumber - 1
      });
  },

  onLoad() {
    var that = this;
    this.setData({
      userMes: wx.getStorageSync('userInfo'),
    })
    var type1= [{}];
    var type2= [{}];
    var type3= [{}];
    var type4= [{}];
    var list = [
      {"name":"食品","id":"0",type1},
      {"name":"饮料","id":"1",type2},
      {"name":"生活用品","id":"2",type3},
      {"name":"其他","id":"3",type4}
    ];

    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    //获取左侧列表
    wx.request({
      url: "http://localhost:8080/wechat/goods",
      method: "GET",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Content-Type': 'application/json'
        // 'Authorization': that.data.token
      },

      success: function (res) {
        var arr=new Array();
        console.info(res.statusCode+1);
        console.info(res.data);
        console.info(that.data.userMes);
        var a=0; var b=0; var c=0; var d=0;  //用于遍历商品的type
        if (res.statusCode == 200) {

          for (var index in res.data){
           let type=res.data[index].type;
           if (type=='食品'){
             type1[a] = {};
             type1[a].name = res.data[index].name;
             type1[a].picUrl = res.data[index].pic;
             type1[a].price = res.data[index].price_out;
             type1[a].id = res.data[index].goods_id;
             a++;
           }
           else if (type=='饮料'){
            type2[b] = {};
            type2[b].name = res.data[index].name;
            type2[b].picUrl = res.data[index].pic;
            type2[b].price = res.data[index].price_out;
            type2[b].id = res.data[index].goods_id;
            b++;
          }
          else if (type=='生活用品'){
            type3[c] = {};
            type3[c].name = res.data[index].name;
            type3[c].picUrl = res.data[index].pic;
            type3[c].price = res.data[index].price_out;
            type3[c].id = res.data[index].goods_id;
            c++;
          }
          else{
            type4[d] = {};
            type4[d].name = res.data[index].name;
            type4[d].picUrl = res.data[index].pic;
            type4[d].price = res.data[index].price_out;
            type4[d].id = res.data[index].goods_id;
            d++;
          }
          }

          that.setData({
            list: list,
            listCur: list[0],
            type1: type1,
            type2: type2,
            type3: type3,
            type4: type4,
          })
          // console.info("导入结果");
          // console.info(that.data.leftname);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期1，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
    })

  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  }
})